﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JKF.Substructure;
using SFWS.XHandler.Core;

namespace JKF.HandlerLayer
{
    /// <summary>
    /// Usage to Show Error Message to Client json obj
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
    public class Exp2ClientAttribute : Attribute
    {
    }

    public static class HandlerExt
    {
        public static void HandleRequest(this AHttpHandler handler, string actionName)
        {
            var methord = handler.GetType().GetMethod(actionName);
            if (methord.GetCustomAttributes(typeof(Exp2ClientAttribute), false).Length > 0)
            {
                try
                {
                    methord.Invoke(handler, null);
                    return;
                }
                catch (Exception exp)
                {
                    if (exp.InnerException != null)
                    {
                        handler.Context.Response.Write(new ResponseInf { Success = false, Message = exp.InnerException.Message }.ToJson());
                        return;
                    }
                    throw exp;
                }
            }
            methord.Invoke(handler, null);
        }
    }

    /// <summary>
    /// 根据此记录相应Log
    /// </summary>
    public enum LogType
    {
        Add,
        Edit,
        Delete
    }
}
